package com.dvdfe8.crochet.modelo.dao;

import com.dvdfe8.crochet.modelo.entity.Amigurumi;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class AmigurumiDaoImpl implements AmigurumiDao {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    @Transactional
    public void crear(Amigurumi amigurumi) {
        sessionFactory.getCurrentSession().saveOrUpdate(amigurumi);
    }

    @Override
    @Transactional
    public List<Amigurumi> listarTodos() {
        String hql = "from Amigurumi";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return query.list();
    }

    @Override
    @Transactional
    public void eliminar(Amigurumi amigurumi) {
        sessionFactory.getCurrentSession().delete(amigurumi);
    }

    @Override
    @Transactional
    public Amigurumi getAmigurumiById(int id) {
        String hql = "from Amigurumi where id=" + id;
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return (Amigurumi) query.list().get(0);
    }

    @Override
    @Transactional
    public Amigurumi getAmigurumiByNombre(String nombre) {
        String hql = "from Amigurumi where nombre=" + nombre;
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return (Amigurumi) query.list().get(0);
    }
    
}
